package net.gd.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import net.gd.utils.PageModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/** 
 * @author Uriel 
 * @date 2018年2月2日 下午11:09:51 
 * @version 1.0 
*/
@Service
public class CommonService {
	/**
	   * @description 数据分页展示通用模块
	   * @author Uriel
	   * @date 2017年7月24日 下午3:05:21
	   * @version V3.0
	   * @param eventJsonArray
	   * @param page
	   * @param current
	   * @param PAGESIZE
	   * @return eventObject
	   */
	  public JSONObject jsonProcessing(JSONArray eventJsonArray, int page, String current, Integer PAGESIZE)
	  {
	    JSONObject eventObject = new JSONObject();
	    PageModel pageModel = new PageModel();
	    pageModel.setPageNo(page);
	    pageModel.setPageSize(PAGESIZE.intValue());
	    pageModel.setTotalRecords(eventJsonArray.size());
	    pageModel.setList(eventJsonArray);

	    JSONObject pageObj = new JSONObject();
	    pageObj.accumulate("current", page);
	    pageObj.accumulate("pagesize", PAGESIZE);
	    pageObj.accumulate("total", pageModel.getTotalPages());
	    pageObj.accumulate("totalLines", eventJsonArray.size());

	    List eventList = new ArrayList();
	    if (eventJsonArray.size() > 0)
	    {
	      int firstIndex = (page - 1) * PAGESIZE.intValue();
	      int toIndex = page * PAGESIZE.intValue();
	      if (eventJsonArray.size() < toIndex) {
	        toIndex = eventJsonArray.size();
	      }
	      if (firstIndex > toIndex) {
	        firstIndex = 0;
	        pageModel.setPageNo(1);
	      }
	      eventList = eventJsonArray.subList(firstIndex, toIndex);
	    } else {
	      System.out.println("此次查询没有返回结果！");
	    }
	    eventObject.accumulate("result", eventList);
	    eventObject.accumulate("page", pageObj);
	    return eventObject;
	  }
}
